/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
/*
 * LOS_WD.c
 *
 *  Created on: 2024年1月7日
 *      Author: 64435
 */

#include <LOS_WDT.h>

uint32_t DWDRST_CNT = 0;

/** @fn void LOS_WDT_DWDInit()
*   @brief 软件看门狗初始化
*   @note 已配置为0.4s狗咬
*/
void LOS_WDT_DWDInit()
{
    uint32_t BUF = 0;
    LOS_EnterAdminMode();
    //先从FLASH读复位了几次
    // memcpy(&DWDRST_CNT,0XF0206000,4);
    //再判断有没有发生狗咬复位
    // BUF = rtiREG1->WDSTATUS;
    // if (BUF & (1<<1))
    // {
    //     DWDRST_CNT++;
    //     LOS_Flash_EraseSector(Fapi_FlashBank7,6);
    //     LOS_Flash_WriteData(Fapi_FlashBank7, 6, 0, &DWDRST_CNT, 4);
    // }
    rtiREG1->WDSTATUS = 0xFFU;
	rtiREG1->DWDPRLD = 3661;  //置为0.4s
    LOS_ExitAdminMode();
}

/** @fn void LOS_WDT_DWDStart()
*   @brief 软件看门狗启动
*   @note 启动后无法通过软件关闭
*/
void LOS_WDT_DWDStart()
{
    LOS_EnterAdminMode();
    rtiREG1->DWDCTRL = 0xA98559DAU;
    LOS_ExitAdminMode();
}

/** @fn void LOS_WDT_DWDSYSRST()
*   @brief 系统复位
*   @note 利用软件看门狗，进行一次系统复位
*/
void LOS_WDT_DWDSYSRST()
{
    LOS_EnterAdminMode();
	rtiREG1->WDKEY = 0x0000E51AU;
	rtiREG1->WDKEY = 0x00002345U;
    LOS_ExitAdminMode();
}

/** @fn void LOS_WDT_DWDFeed()
*   @brief 喂软件看门狗
*   @note 
*/
void LOS_WDT_DWDFeed()
{
    LOS_EnterAdminMode();
    rtiREG1->WDKEY = 0x0000E51AU;
	rtiREG1->WDKEY = 0x0000A35CU;
    LOS_ExitAdminMode();
}

/** @fn void LOS_WDT_DWDStart()
*   @brief 喂狗函数
*   @note 使用硬件看门狗时，应在其中添加硬件看门狗喂狗操作，使用软件看门狗时亦然。
*/
void LOS_WDT_Feed()
{
    //LOS_WDT_DWDFeed();
}
